package com.zhq.tool.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {


    public static String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    public static String FORMAT_DATE = "yyyy-MM-dd";

    public static String getNowDate() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String dateNowStr = sdf.format(d);
        return dateNowStr;
    }

    public static String getNowDate(String format, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String convertDate(String dateStr) {
        if (dateStr.contains("-")) {
            String[] dateArray = dateStr.split("-");
            return dateArray[0] + "年" + dateArray[1] + "月" + dateArray[2] + "日";
        }
        return dateStr;
    }

    public static Date convert(String dateStr) {
        String format = "";
        if (dateStr.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}")) {
            format = "YYYY-MM-DD HH:mm";
        } else if (dateStr.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
            format = "YYYY-MM-DD HH:mm:ss";
        } else if (dateStr.matches("\\d{4}-\\d{2}-\\d{2}")) {
            format = "YYYY-MM-DD";
        } else if (dateStr.matches("\\d{4}-\\d{2}")) {
            format = "YYYY-MM";
        }
        System.out.println("format:" + format);
        SimpleDateFormat ft = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = ft.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 当前日期往前推
     *
     * @param d
     * @param day
     * @return
     */
    public  static Date getDateBefore(Date d, int day) {
        Calendar no = Calendar.getInstance();
        no.setTime(d);
        no.set(Calendar.DATE, no.get(Calendar.DATE) - day);
        return no.getTime();
    }

    public static boolean isVerify(String date, String format) {
        if (date.matches(format)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        try {
            System.out.println(getTime("2022-02","2022-03"));
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    public static int getTime(String startDate,String endDate) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Date beString = dateFormat.parse(startDate);
        Date enString = dateFormat.parse(endDate);
        long sub = Math.abs(beString.getTime() - enString.getTime());
        return (int) (sub/1000/60/60/24/28);
    }

    /****
     * @param time
     * 前一天日期
     **/
    public static String getDateYesterday(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            // 注意格式需要与上面一致，不然会出现异常
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (null == date) {
            return "";
        }
        return sdf.format(new Date(date.getTime() - 1 * 24 * 60 * 60 * 1000));
    }


    public static String addMin(int parseInt, String nowDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.parseInt(nowDate.substring(0, 4)), Integer.parseInt(nowDate.substring(4, 6)) - 1,
                Integer.parseInt(nowDate.substring(6, 8)), 0, 0, 0);
        calendar.add(Calendar.MINUTE, parseInt);
        return sdf.format(calendar.getTime());
    }

    public static String getFormatDate(String dates) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        String dateNowStr = sdf1.format(sdf.parse(dates));
        return dateNowStr;
    }

    /**
     * @return
     * @Description 获取本月开始日期
     */
    public static String getThisMonthStartDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstday;
        // 获取前月的第一天
        Calendar cale = null;
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = format.format(cale.getTime());
        return firstday;
    }

    /**
     * @param
     * @return
     * @Description 获取当前日期
     */
    public static String getNowDate(String format) {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String dateNowStr = sdf.format(d);
        return dateNowStr;
    }

    public static Date getHisDate(String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE_TIME);
        Date hisDate = null;
        try {
            hisDate = sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return hisDate;
    }
    public static Date getDate(String dateStr,String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date hisDate = null;
        try {
            hisDate = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return hisDate;
    }
    public static Date getCurrentDate() {
        try {
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            return sdf.parse(sdf.format(now));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate() {
        try {
            String format = "HH:mm:ss";
            SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
            String now = sf.format(new Date());
            Date nowTime = new SimpleDateFormat(format).parse(now);
            Date startTime = new SimpleDateFormat(format).parse("01:00:00");
            Date endTime = new SimpleDateFormat(format).parse("02:00:00");
            if (nowTime.getTime() == startTime.getTime()
                    || nowTime.getTime() == endTime.getTime()) {
                return true;
            }
            Calendar date = Calendar.getInstance();
            date.setTime(nowTime);
            Calendar begin = Calendar.getInstance();
            begin.setTime(startTime);
            Calendar end = Calendar.getInstance();
            end.setTime(endTime);
            if (date.after(begin) && date.before(end)) {
                System.out.println("当前时间：" + now);
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

}
